﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;

namespace Portal
{
    class Common
    {
        public static string CreateFoldersIfNot(SPWeb web, SPList attenlist)
        {
            var year = DateTime.Today.Year.ToString();
            var month = DateTime.Today.ToString("MMM");
            var day = DateTime.Today.Day.ToString();
            web.AllowUnsafeUpdates = true;
            var urlYear = web.Url + "/Lists/" + "Attendance" + "/" + year;
            SPFolder fYear = web.GetFolder(urlYear);
            if (!fYear.Exists)
            {
                SPListItem folderYear = attenlist.Items.Add(attenlist.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, year);
                folderYear["Title"] = year;
                folderYear.Update();
            }

            var urlMonth = web.Url + "/Lists/" + "Attendance" + "/" + year + "/" + month;
            SPFolder fMonth = web.GetFolder(urlMonth);
            if (!fMonth.Exists)
            {
                SPListItem folderMonth = attenlist.Items.Add(urlYear, SPFileSystemObjectType.Folder, month);
                folderMonth["Title"] = month;
                folderMonth.Update();
            }

            var urlDay = web.Url + "/Lists/" + "Attendance" + "/" + year + "/" + month + "/" + day;
            SPFolder fDay = web.GetFolder(urlDay);
            if (!fDay.Exists)
            {
                SPListItem folderDay = attenlist.Items.Add(urlMonth, SPFileSystemObjectType.Folder, day);
                folderDay["Title"] = day;
                folderDay.Update();
            }
            web.AllowUnsafeUpdates = false;
            return urlDay;
        }
        public static void LogPortal(Exception ex)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {


                string cs = "Portal";
                EventLog elog = new EventLog();

                if (!EventLog.SourceExists(cs))
                {
                    EventLog.CreateEventSource(cs, cs);
                }
                elog.Source = cs;
                elog.EnableRaisingEvents = true;
                elog.WriteEntry(ex.ToString(), EventLogEntryType.Error);
                elog.Close();

            });
        }
        public static void AlertMessage(Control ctrl,Type tp, string msg)
        {
            ScriptManager.RegisterStartupScript(ctrl, tp.GetType(), "PortalAlert",
                "UpdateMessage('" + msg + "');", true);
            //$('.wow').css({opacity: 0.0, visibility: "visible"}).animate({opacity: 1.0},2000).animate({opacity: 0.0},2000);
        }
        public static void ExecuteJS(Control ctrl, Type tp, string js)
        {
            
            ScriptManager.RegisterStartupScript(ctrl, tp.GetType(), "PortalInlineJS",
                js, true);
        }
    }
}
